
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>员工管理</title>
    <#include "../common/link.ftl">
    <script>
        $(function () {
            //员工删除
            $(".btn_delete").click(function () {
                var id = $(this).data("id");
                //提示一个确认框
                $.messager.confirm("警告","是否确认删除",function () {
                    //发送ajax请求
                    $.get("/employee/delete.do",{id:id},employeeMessage)
                })
            })
        })
    </script>
    <script>
        /*完成效果，当三条都挨个被选中时，那么最上面的全选也是被选中的
        * 如果取消三个记录中的一个，那么最上面的也要取消选中
        * */
        $(function () {
            //全选/全不选
            $("#allCb").click(function () {
                //获取当前复选框的状态checked
                //设置到table中的所有复选框
                $('.cb').prop('checked',$(this).prop('checked'));
            })
            $('.cb').click(function () {
                //获取已选中的复选框,判断一下是否等于table中的所有复选框的数量
                $('#allCb').prop('checked',$('.cb:checked').length == $('.cb').length);

            });
            //获取被选中的id
            $('.btn_abtchDelete').click(function () {
                //获取勾选的数据
                //如果没有数据，需要先提示一下，请选择要删除的数据
                if($('.cb:checked').length == 0){
                    $.messager.alert("警告","请选择要删除的记录！");
                    return;
                }
                //获取选中的记录ID，保存到数组中
                var cbs = $('.cb:checked');
                var ids = [];
                //方式一
              /*  $.each(cbs,function (index,ele) {
                    ids.push($(ele).data('id'));
                })*/
                //方式二
                var ids = $.map(cbs,function (ele) {
                    return $(ele).data('id');
                });
                /*由于jquery的问题，传递到后端的数组，jquery会再封装一层括号{}，
                * 在原有[]数组的基础上再封装一个{}，就变成了{[4,5]}
                * */
                //这个设置后jquery就不会在原有数组的基础上再次封装一个{}了
                jQuery.ajaxSettings.traditional = true;
              console.log(ids);
              $.messager.confirm("警告","请确认是否删除选中的记录",function (data) {
                    $.get("/employee/abtchDelete.do",{ids:[ids]},employeeMessage);
                })
            })

            $(".btn-import").click(function () {
                $("#myModal").modal('show');
            })
            //上传处理(模板下载)
            $(".btn-submit").click(function () {
                $("#editForm").ajaxSubmit(employeeMessage);
            })
        })

    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <#include "../common/navbar.ftl">
    <!--菜单回显-->
    <#assign var="employee"/>
    <#include "../common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>员工管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询--->
                <div style="margin: 10px;">
                    <form class="form-inline" id="searchForm" action="/employee/list.do" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="1">
                        <div class="form-group">
                            <label for="keyword">关键字:</label>
                            <input type="text" class="form-control" id="keyword" name="keyword" value="${qo.keyword!}" placeholder="请输入姓名/邮箱">
                        </div>
                        <div class="form-group">
                          <label for="dept"> 部门:</label>
                            <select class="form-control" id="dept" name="dept_id">
                                <option value="-1">全部</option>
                                <#list depts as d>
                                    <option value="${d.id!}">${d.name!}</option>
                                </#list>
                            </select>
                             <script>
                               <#--  $("#dept option[value='${qo.deptId}']").prop("selected", true);-->
                               $("#dept").val(${qo.dept_id});
                            </script>
                        </div>
                        <button id="btn_query" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> 查询</button>
                        <a href="/employee/input.do" class="btn btn-success btn_redirect">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>
                        <a href="#" class="btn btn-danger btn_abtchDelete">
                            <span class="glyphicon glyphicon-trash"></span> 批量删除
                        </a>
                        <#--导入   导出-->
                        <a href="/employee/exportxls.do" class="btn btn-warning">
                            <span class="glyphicon glyphicon-export"></span>导出
                        </a>
                        <a href="#" class="btn btn-warning btn-import">
                            <span class="glyphicon glyphicon-import"></span> 导入
                        </a>

                    </form>
                </div>
                <table class="table table-hover table-bordered">
                    <thead>
                    <tr>
                        <th><input type="checkbox" id="allCb"></th>
                        <th>编号</th>
                        <th>名称</th>
                        <th>email</th>
                        <th>年龄</th>
                        <th>部门</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <#list result.list as entity>
                        <tr>
                            <td><input type="checkbox" class="cb" data-id="${entity.id}"></td>
                            <td>${entity_index+1}</td>
                            <td>${entity.name!}</td>
                            <td>${entity.email!}</td>
                            <td>${entity.age!}</td>
                            <td>${(entity.dept.name)!}</td>
                            <td>
                                <a href="/employee/input.do?id=${entity.id}" class="btn btn-info btn-xs btn_redirect">
                                    <span class="glyphicon glyphicon-pencil"></span> 编辑
                                </a>
                                <a href="#" data-id="${entity.id}" class="btn btn-danger btn-xs btn_delete">
                                    <span class="glyphicon glyphicon-trash"></span> 删除
                                </a>
                            </td>
                        </tr>
                    </#list>
                </table>
                <!--分页-->
                <#include "../common/page.ftl">
            </div>
        </section>
    </div>
    <#include "../common/footer.ftl">
</div>

<#--模态框-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">员工导入</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/employee/importXls.do" method="post" id="editForm" enctype="multipart/form-data">
                    <input type="hidden" name="id">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-3 control-label">导入文件：</label>
                        <div class="col-sm-6">
                            <input type="file" name="file">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <#--地址写下载模板文件躲在的路径即可-->
                        <div class="col-sm-3">
                            <div class="col-sm-6">
                                <a href="/xlstemplates/employee_import.xls" class="btn btn-success">
                                    <span class="glyphicon glyphicon-export"></span> 下载模板
                                </a>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-submit">保存</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>
